package com.finalproject.ui;
/**
 * DietaryProfile class for UI
 */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.finalproject.entity.CardHolder;
import com.finalproject.entity.CardHolderDatabase;
import com.finalproject.entity.CardHolderDatabase.CardHolderDatabaseObserver;
import com.finalproject.entity.DietaryProfile;
import com.finalproject.entity.DietaryProfile.DietaryProfileObserver;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

public class UIDietaryProfile extends JPanel implements DietaryProfileObserver,CardHolderDatabaseObserver{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

//	private JLabel usedCaloriesLabel;
//	private JLabel usedSatfatLabel;
//	private JLabel usedCholestrolLabel;
//	private JLabel usedSodiumLabel;
//	private JLabel usedCarbsLabel;

	private JTextField caloriesTextField;
	private JTextField satFatTextField;
	private JTextField cholestrolTextField;
	private JTextField sodiumTextField;
	private JTextField carbsTextField;
	private JTextField proteinTextField;
	
	private JButton updateProfileButton;

	public UIDietaryProfile() {
		super(new BorderLayout());
		CardHolderDatabase.getInstance().addCardHolderDatabaseObserver(this);
	
		setBorder(BorderFactory.createTitledBorder(""));
		
		
	

	}

	
	@Override
	public void onLogin(CardHolder cardHolder, Integer familyId) {
		loadDietaryProfile(cardHolder.getDietary());
		cardHolder.getDietary().addDietaryProfileObserver(this);
		setBorder(BorderFactory.createTitledBorder("My Dietary Profie "));
		
		
	}

	@Override
	public void onLogout(CardHolder cardHolder, Integer familyId) {
		removeAll();
		setBorder(BorderFactory.createTitledBorder(""));
		
		
	}
	@Override
	public void onUpdate(DietaryProfile dietaryProfile) {
		
		loadDietaryProfile(dietaryProfile);
	}
	
	
	private void loadDietaryProfile(final DietaryProfile dietaryProfile) {
		removeAll();

		setBorder(BorderFactory.createTitledBorder("My Dietary Profile"));
		
		DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout(""));
		builder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		builder.appendColumn("left:pref");
		builder.appendColumn("3dlu");
		builder.appendColumn("fill:max(pref; 100px)");
		builder.appendColumn("5dlu");
		builder.appendColumn("left:pref");
		builder.appendColumn("3dlu");
		builder.appendColumn("fill:max(pref; 100px)");
		
		String calories = new Integer(dietaryProfile.getCalories()).toString();
		String carbs = new Integer(dietaryProfile.getCarbs()).toString();
		String cholestrol = new Integer(dietaryProfile.getCholestrol()).toString();
		String fat = new  Integer(dietaryProfile.getFat()).toString();
		String protein = new  Integer(dietaryProfile.getProtein()).toString();
		String sodium = new Integer(dietaryProfile.getSodium()).toString();

		caloriesTextField = new JTextField(calories);
		builder.append("Max Calories:", caloriesTextField, new JLabel("cal"));
		builder.nextLine();
		
		satFatTextField =  new JTextField(fat);
		builder.append("Max Fats:", satFatTextField,new JLabel("g"));
		builder.nextLine();
		
		
		
		
		

		cholestrolTextField =  new JTextField(cholestrol);
		builder.append("Max Cholestrols:", cholestrolTextField, new JLabel("mg"));
		builder.nextLine();
		
		
		
	
		
		sodiumTextField = new JTextField(sodium);
		builder.append("Max Sodiums:", sodiumTextField, new JLabel("mg"));
		builder.nextLine();
		
		carbsTextField =  new JTextField(carbs);
		builder.append("Max carbs:", carbsTextField, new JLabel("g"));
		builder.nextLine();
		
		
		
		proteinTextField = new JTextField(protein);
		builder.append("Max Proteins:", proteinTextField, new JLabel("g"));
		builder.nextLine();
		
		
		updateProfileButton = new JButton("Update my Dietary Profile");
		updateProfileButton.setPreferredSize(new Dimension(200,50));
		updateProfileButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				saveDietaryProfile(dietaryProfile);
				
			}
		});
		builder.nextColumn();
		builder.append("",updateProfileButton);
		
		add(builder.getPanel(), BorderLayout.CENTER);
		
		add(builder.getPanel());
		repaint();
		revalidate();
	}
	
	
	
	private void saveDietaryProfile(DietaryProfile dietaryProfile)  {
		
		int calories = 0;
		int carbs = 0;
		int cholestrol = 0;
		int fat = 0;
		int protein = 0;
		int sodium = 0;
		try {
			
			calories = Integer.parseInt(caloriesTextField.getText());
			

		} catch(Exception e) {
			UIHelper.showErrorMessage(this, "Invalid Max Calories");
		}
		
		
		
	
		try {
			fat = Integer.parseInt(satFatTextField.getText());
			
		} catch(Exception e) {
			UIHelper.showErrorMessage(this, "Invalid Max Fat");
		}
		
		try {
			cholestrol = Integer.parseInt(cholestrolTextField.getText());
			
		} catch(Exception e) {
			UIHelper.showErrorMessage(this, "Invalid Max Cholestrol");
		}
		
		
	
		
		
		
		
		try {
			sodium = Integer.parseInt(sodiumTextField.getText());
		} catch(Exception e) {
			UIHelper.showErrorMessage(this, "Invalid Sodium");
		}
		
		try {
			carbs = Integer.parseInt(carbsTextField.getText());
			

		} catch(Exception e) {
			UIHelper.showErrorMessage(this, "Invalid Max Carbs");
		}
		
		try {
			 protein = Integer.parseInt(proteinTextField.getText());
			
		} catch(Exception e) {
			UIHelper.showErrorMessage(this, "Invalid Protein");
		}
		
		dietaryProfile.updatePreference(calories, fat, cholestrol, sodium, carbs, protein);
		UIHelper.showConfirmMessage(this, "Dietary Profile updated successfully");
	}

	

}
